<?php
/** @var $model backend\models\Goods */
/** @var $goodsIntro backend\models\GoodsIntro */
use backend\models\Goods;
use xj\uploadify\Uploadify;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Html;
use yii\web\JqueryAsset;
use yii\web\JsExpression;

$this->title = "商品添加";

$this->params['breadcrumbs'][] = ['label'=>'商品列表','url'=>['goods/list']];

$this->params['breadcrumbs'][]=$this->title;



$form = ActiveForm::begin();
echo $form->field($model,'name');
echo $form->field($model,'logo')->hiddenInput(['id'=>'uploadImg']);



echo Html::img($model->imgsrc(),['id'=>'showImg','style'=>'max-height:100px']);
echo Html::fileInput('imginfo', NULL, ['id' => 'test']);
echo Uploadify::widget([
    'url' => yii\helpers\Url::to(['goods/s-upload']),
    'id' => 'test',
    'csrf' => true,
    'renderTag' => false,
    'jsOptions' => [
        'width' => 120,
        'height' => 40,
        'onUploadError' => new JsExpression(<<<EOF
function(file, errorCode, errorMsg, errorString) {
    console.log('The file ' + file.name + ' could not be uploaded: ' + errorString + errorCode + errorMsg);
}
EOF
        ),
        'onUploadSuccess' => new JsExpression(<<<EOF
function(file, data, response) {
    data = JSON.parse(data);
    if (data.error) {
        console.log(data.msg);
    } else {
        
        //给隐藏域赋值
        $("#uploadImg").val(data.fileUrl);
        //回显图片
        //console.dir($("#showImg"));
        
        $("#showImg").attr('src',data.fileUrl);
    }
}
EOF
        ),
    ]
]);

echo $form->field($model,'goods_category_id')->hiddenInput();
//输出商品的分类的树状图
echo '<div>
    <ul id="treeDemo" class="ztree"></ul>
</div>';
echo $form->field($model,'brand_id')->dropDownList(array_merge([''=>''],Goods::brandsInfo()));
echo $form->field($model,'market_price');
echo $form->field($model,'shop_price');
echo $form->field($model,'stock');
echo $form->field($model,'is_on_sale',['inline'=>true])->radioList(Goods::$saleStatus);
echo $form->field($model,'sort');
echo $form->field($model,'status',['inline'=>true])->radioList(Goods::$statusValue);
//echo $form->field($goodsIntro,'content')->textarea();
echo $form->field($goodsIntro,'content')->widget('kucha\ueditor\UEditor',[]);

echo Html::submitButton('提交',['class'=>'btn btn-primary']);

ActiveForm::end();



$cateInfo = Goods::categorysInfo();

//注册js文件
$this->registerJsFile('@web/zTree/js/jquery.ztree.core.js',['depends'=>JqueryAsset::className()]);
//注册样式文件
$this->registerCssFile('@web/zTree/css/zTreeStyle/zTreeStyle.css');
//注册js代码
$jsfile = <<<DOG

    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parent_id",
                    rootPId: 0
                }
            },
            callback:{
                onClick: function(event, treeId, treeNode){
                    $("#goods-goods_category_id").val(treeNode.id);
                }
            },
        };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
        var zNodes = {$cateInfo};
    //好像是获得当前的树对象
        zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
   
    //设定是否展开树节点
        zTreeObj.expandAll(false);
    //编辑室自动选中当前分类的父节点  parent_id  id
    //当前节点的parent_id ={$model->goods_category_id};
    //当前节点的父级节点的id是当前节点的parent_id
    //通过查找的到当前对象 的父级对象
    if('$model->goods_category_id'){
        var node = zTreeObj.getNodeByParam("id",'{$model->goods_category_id}',null);
         //把找到的对象设置为选中状态
         zTreeObj.selectNode(node);
    }
DOG;

//注册js文件代码
$this->registerJs($jsfile);


?>








